class VideoAlbumsController < ApplicationController
  before_filter :require_user
	def index
    if current_user.role.name == 'admin'
      @video_albums = VideoAlbum.order("order_no ASC")
    else
      @video_albums = current_user.video_albums.order("order_no ASC")
    end
  end

  def show
    @video_album = VideoAlbum.find(params[:id])
  end

  def new
    @video_album = current_user.video_albums.new
  end

  def create
    @video_album = current_user.video_albums.new(params[:video_album])
    @video_album.order_no = VideoAlbum.last.blank? ? "0" : VideoAlbum.last.id 
    if @video_album.save
      redirect_to @video_album, :notice => "Successfully created video album."
    else
      render :action => 'new'
    end
  end

  def edit
    @video_album = VideoAlbum.find(params[:id])
  end

  def update
    @video_album = VideoAlbum.find(params[:id])
    if @video_album.update_attributes(params[:video_album])
      redirect_to @video_album, :notice  => "Successfully updated video album."
    else
      render :action => 'edit'
    end
  end

  def change_position
    @video_album_1 = VideoAlbum.find(params[:id])
    @video_album_2 = VideoAlbum.find(params[:change_id])
    if !@video_album_1.blank? && !@video_album_2.blank?
      @video_album_1.order_no,@video_album_2.order_no = @video_album_2.order_no,@video_album_1.order_no
      @video_album_1.save
      @video_album_2.save
    end
    redirect_to :action => :index
  end

  def destroy
    @video_album = VideoAlbum.find(params[:id])
    @video_album.destroy
    redirect_to video_albums_url, :notice => "Successfully destroyed video album."
  end
end
